<div ng-controller="InfoCollection_ListC">
    <div class="bg-light lter b-b wrapper-md">
        <div class="row">
            <h1 class="m-n font-thin h3 col-lg-8 col-md-8 col-sm-8 col-xs-12">钢厂信息日报表</h1>
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 text-right">
                选择日期<input type="date" ng-model="d"   ng-change="getData(d)" />
                <button class="btn btn-default" ng-click="$state.go('app.InfoCollection_List')">返回</button>
            </div>
        </div>
    </div>
    <div id="hot" class="text-center"></div>
</div>
<script>
    angular.module('app')//非标优惠
            .controller("InfoCollection_ListC", function ($scope, $state, $http) {
        $scope.d = new Date();
                var dataObject = [
                    {Type: null, SupplierName: null, AgentName: null, Name: null,	Price: null, Stock: null, Volume: null, Remark: null}
                ];
                var hotElement = document.querySelector('#hot');
                var hotElementContainer = hotElement.parentNode;
                var hotSettings = {
                    data: dataObject,
                    columns: [
                        {
                            data: 'SupplierName',
                            type: 'text',
//                            width: 30
                        },
                        {
                            data: 'Type',
                            type:'dropdown',
                            source: ['建材', '原材料', '供应商'],
//                            width: 30

                        },
                        {
                            data: 'AgentName',
                            type: 'text',
//                            width: 30

                        },
                        {
                            data: 'Name',
                            type:'dropdown',
                            source: ['螺纹钢', '线材', '盘螺', '总计', '矿粉', '焦炭', '钢坯'],
//                            width:20
                        },
                        //{
                        //    data: 'Price',
                        //    type: 'numeric',
                        //    format: '0.0000'
                        //},
                        {
                            data: 'Stock',
                            type: 'numeric',
                            format: '0.00',
//                            width: 10
                        },
                        {
                            data: 'Volume',
                            type: 'numeric',
                            format: '0.00',
//                            width: 10

                        },
                        {

                            data: 'Remark',
                            type: 'text'
//                format: '0.00%'
                        }
//            {
//                data: 'InfoDate',
//                type: 'date',
//                dateFormat: 'MM/DD/YYYY'
//            }
                    ],
                    manualColumnResize:true,
//                    colWidths:[
//                            10,
//                            10,
//                            10,
//                            10,
//                            10,
//                        10
//
//                    ],
//                    mergeCells: [
//                        {row:0, col:0, rowspan:5, colspan:1},
//                        {row:5, col:0, rowspan:4, colspan:1}
//                    ],
//                    cell: [
//                        {row: 0, col: 0, className: "htRight"},
//                        {row: 1, col: 1, className: "htLeft htMiddle"},
//                        {row: 3, col: 4, className: "htLeft htBottom"}
//                    ],
                    className:"htMiddle",
                    readOnly:true,
                    stretchH: 'all',
                    //width: '100%',
                    autoWrapRow: true,
                    height: 800,
//        maxRows: 22,
                    minSpareRows:2,
                    rowHeaders: true,
                    colHeaders: [
                        '厂家名称',
                        '类别',
                        '贸易商名称',
                        '名称',
                        //'基价',
                        '库存量(吨)',
                        '出货情况(车)',
                        '备注'
//            '时间'
                    ]
                };

                var hot = new Handsontable(hotElement, hotSettings);
                var mergeFun=function(begin,end,param,col,list,data){

                    for(var j = begin+1; j <=end+1;j++){

                        if(data[j < data.length ? j : j-1][param]!=data[begin < data.length ? begin : begin-1][param]||j+1>end)
                        {
                            if((j-begin)>0)
                            {
                                rowcount = (begin == 0 ? begin : begin - 1);
                                mer = { row: rowcount, col: col, rowspan: j - rowcount, colspan: 1 };
                                list.push(mer);
                            }
                            begin=j+1;
                        }
                    }
                };
                $scope.getData = function (data) {
                    $http.get("/api/InfoCollection/List", { params: { rows: 9999, dt: $scope.d } }).then(
                            function (res) {
                                //hot.loadData(res.data.Data);
                                console.log(res);

                                hotSettings.data = res.data.Data;
                                var begin = 0;

                                var mergeCells = [];
                                var data = res.data.Data;

                                for (var i = 1; i < data.length; i++) {
                                    if (data[i].SupplierName != data[begin].SupplierName || i + 1 == data.length) {
                                        if ((i - begin) >= 0) {
                                            rowcount = begin != 0 ? begin - 1 : begin;
                                            span = (i + 1 == data.length ? (begin == 0 ? i - begin + 1 : i - begin + 2) : (begin == 0 ? i - begin : i - begin + 1));
                                            var mer = { row: rowcount, col: 0, rowspan: span, colspan: 1 };
                                            mergeCells.push(mer);                                           console.log(begin);
                                            mergeFun(begin, i, "Type", 1, mergeCells, data);

                                        }
                                        begin = i + 1;
                                    }
                                }
                                hotSettings.mergeCells = mergeCells;
                                //                                console.log(mergeCells);
                                hot = new Handsontable(hotElement, hotSettings);
                                //   tabMager(hot, 1, 1, 2, 3);
                            }
                    );
                };

                $scope.getData();
            });

    function tabMager(hot, row, col, rowspan, colspan) {
      
            if(hot.mergeCells.mergedCellInfoCollection.length > 0) {
                for(var k=0; k<hot.mergeCells.mergedCellInfoCollection.length; k++) {
                    if(hot.mergeCells.mergedCellInfoCollection[k].row == row &&
                         hot.mergeCells.mergedCellInfoCollection[k].col == col) {
                        hot.mergeCells.mergedCellInfoCollection.splice(k,1);
                        return;
                    }else{
                        hot.mergeCells.mergedCellInfoCollection.push({"row": row, "col": col,
                            "rowspan": rowspan, "colspan": colspan});
                        break;
                    }
                }
            } else {
                hot.mergeCells.mergedCellInfoCollection.push({"row": row, "col": col, "rowspan": rowspan, "colspan": colspan});
            }
    }
</script>
